//package com.shqcjd.yuhuanglass.ui.activity.videotape;
//
//import android.Manifest;
//import android.app.Activity;
//import android.app.Service;
//import android.content.Context;
//import android.content.Intent;
//import android.os.IBinder;
//import android.os.SystemClock;
//import android.util.Log;
//
//import com.orhanobut.logger.Logger;
//import com.shqcjd.yuhuanglass.R;
//import com.shqcjd.yuhuanglass.api.ApiService;
//import com.shqcjd.yuhuanglass.app.AppConstant;
//import com.shqcjd.yuhuanglass.app.MyApp;
//import com.shqcjd.yuhuanglass.helper.rxjavahelper.RxObserver;
//import com.shqcjd.yuhuanglass.helper.rxjavahelper.RxSchedulersHelper;
//import com.shqcjd.yuhuanglass.model.ResponseData;
//import com.shqcjd.yuhuanglass.model.eventbus.CreateVideotape;
//import com.shqcjd.yuhuanglass.model.eventbus.FinishActivityEvent;
//import com.shqcjd.yuhuanglass.model.eventbus.FinishVideotape;
//import com.shqcjd.yuhuanglass.model.eventbus.VideoReleaseEvent;
//import com.shqcjd.yuhuanglass.util.PrefUtils;
//import com.shqcjd.yuhuanglass.util.ToastUtils;
//import com.tbruyelle.rxpermissions2.RxPermissions;
//
//import org.greenrobot.eventbus.EventBus;
//import org.greenrobot.eventbus.Subscribe;
//import org.greenrobot.eventbus.ThreadMode;
//import org.json.JSONObject;
//
//import java.util.HashMap;
//import java.util.Map;
//
//import io.agora.AgoraAPIOnlySignal;
//import io.agora.rtc.IRtcEngineEventHandler;
//import io.agora.rtc.RtcEngine;
//import io.agora.rtc.video.VideoEncoderConfiguration;
//import io.reactivex.disposables.Disposable;
//import io.reactivex.functions.Consumer;
//
///**
// * 全程录像服务
// */
//public class VideotapeService extends Service{
//    private Context mContext;
//    private RtcEngine mRtcEngine;
//    private Activity activity;
//    private String room_id;
//    private int user_id;
//    private int request_code;
//    private String kks;
//    private String managerUserId;
//    private String problemFeedbackId;
//    private String taskCode;
//    private String taskId;
//
//
//    public VideotapeService() {
//    }
//
//    @Override
//    public IBinder onBind(Intent intent) {
//        // TODO: Return the communication channel to the service.
//        throw new UnsupportedOperationException("Not yet implemented");
//    }
//
//    @Override
//    public void onCreate() {
//        super.onCreate();
//        mContext = this;
//        EventBus.getDefault().register(this);//订阅
//
//    }
//
//    @Override
//    public int onStartCommand(Intent intent, int flags, int startId) {
//        return super.onStartCommand(intent, flags, startId);
//    }
//
//
//
//    @Override
//    public void onDestroy() {
//        super.onDestroy();
//        EventBus.getDefault().unregister(this);//解除订阅
//    }
//
//
////    /**
////     * 创建全程录像
////     */
////    @Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行
////    public void onCreateVideotapeEvent(CreateVideotape videotape) {
////        Logger.i("全程录像---创建");
////        activity = videotape.activity;
////        kks = videotape.kks;
////        managerUserId = videotape.managerUserId;
////        problemFeedbackId = videotape.problemFeedbackId;
////        taskCode = videotape.taskCode;
////        room_id = videotape.roomId;
////        user_id = videotape.userId;
////        taskId = videotape.taskId;
////        checkPermissions(activity);
////
////    }
////
////    boolean flag = true;
////    /**
////     * 结束全程录像
////     */
////    @Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行
////    public void onFinishVideotapeEvent(FinishVideotape event) {
////        Logger.i("打开相机0"+event.code);
////        request_code = event.code;
////        if (request_code == AppConstant.ABNORMAL_CODE) {
////            flag = true;
////        }
////
////        leaveChannel();
////
////
////
//////        guideHandle("1");//	0:开始录制、1:结束录制
////    }
//
//
//    private void checkPermissions(Activity activity) {
//        RxPermissions permissions = new RxPermissions(activity);
//        permissions.setLogging(true);
//        permissions.request(Manifest.permission.RECORD_AUDIO,
//                Manifest.permission.CAMERA,
//                Manifest.permission.WRITE_EXTERNAL_STORAGE)
//                .subscribe(new Consumer<Boolean>() {
//                    @Override
//                    public void accept(Boolean aBoolean) throws Exception {
//                        if (aBoolean) {
//                            initAgoraEngineAndJoinChannel();
//                        } else {
//                            ToastUtils.showShort(mContext, "缺少全程录像必要权限");
//                        }
//                    }
//                });
//    }
//
//    private void initAgoraEngineAndJoinChannel() {
//        //初始化视频
//        initializeAgoraEngine();     // 步骤1
//        setupVideoProfile();         // 步骤2
////        MyApp.the().setOnAgoraEngineInterface(this);
//        joinChannel();
//    }
//
//
//    private void setupVideoProfile() {
//        mRtcEngine.enableVideo();
//
////        mRtcEngine.setVideoProfile(Constants.VIDEO_PROFILE_360P, false);//视频分辨率
//
//        VideoEncoderConfiguration.VideoDimensions vd_1280x720 = VideoEncoderConfiguration.VD_1280x720;
//        VideoEncoderConfiguration.FRAME_RATE frameRateFps15 = VideoEncoderConfiguration.FRAME_RATE.FRAME_RATE_FPS_15;
//        int standardBitrate = VideoEncoderConfiguration.STANDARD_BITRATE;
//        VideoEncoderConfiguration.ORIENTATION_MODE orientationModeAdaptive = VideoEncoderConfiguration.ORIENTATION_MODE.ORIENTATION_MODE_FIXED_LANDSCAPE;
//        VideoEncoderConfiguration videoEncoderConfiguration = new VideoEncoderConfiguration(vd_1280x720, frameRateFps15, standardBitrate, orientationModeAdaptive);
//        mRtcEngine.setVideoEncoderConfiguration(videoEncoderConfiguration);
//    }
//
//    private void initializeAgoraEngine() {
//        mRtcEngine = MyApp.the().getmRtcEngine();
//        if (mRtcEngine != null) {
//            mRtcEngine.setLogFile("/sdcard/sdklog.txt");
//        }
//    }
//
//    private void joinChannel() {
//        mRtcEngine.leaveChannel();
//        //保存打开全程录像记录
//        PrefUtils.setBoolean(this, PrefUtils.VIDEOTAPE, true);
//        mRtcEngine.joinChannel(null, room_id, "Extra Optional Data", user_id); // if you do not specify the uid, we will generate the uid for you
//    }
//
//
//    //退出
//    // Tutorial Step 6
//    private void leaveChannel() {
//        //用户必须调用 leaveChannel() 方法退出当前通话，才能进入下一个频道。
//        if (mRtcEngine!=null) {
//            Logger.i("释放资源");
//            mRtcEngine.stopPreview();
//            mRtcEngine.leaveChannel();//离开频道
//        }
//
//    }
//
//
//    /**
//     * 后台创建远程指导接口
//     */
//    private void createGuide() {
////        Map<String, String> map = new HashMap<>();
////        map.put("user_id", user_id + "");
////        map.put("expert_id", "");
////        map.put("equipment_id", equipment_id);
////        map.put("flights_task_id", flights_task_id);
////        map.put("room_id", room_id);
//
//        Map<String, String> map = new HashMap<>();
//        map.put("userId", user_id + "");
//        map.put("managerUserId", managerUserId+"");
//        map.put("roomId", room_id);
//        map.put("kks", kks);
//        map.put("problemFeedbackId", problemFeedbackId+"");
//        map.put("taskCode", taskCode);
//        map.put("taskId", taskId);
//
//        ApiService.createGuide(new JSONObject(map))
//                .compose(RxSchedulersHelper.io_main())
//                .subscribe(new RxObserver<ResponseData>() {
//                    @Override
//                    public void _onSubscribe(Disposable d) {
//                        super._onSubscribe(d);
//
//                    }
//
//                    @Override
//                    public void _onNext(ResponseData retData) {
//                        if (retData != null && retData.retCode == 10000) {
//                            guideHandle("0");
//                        }
//
//
//                    }
//
//                    @Override
//                    public void _onError(String errorMessage) {
//                    }
//
//                    @Override
//                    public void onComplete() {
//                        super.onComplete();
//                    }
//                });
//    }
//
//    private void guideHandle(String handle) {
//        Map<String, String> map = new HashMap<>();
//        map.put("roomId", room_id);
//        map.put("operation", handle);//	0:开始录制、1:结束录制
//        map.put("guideStatus", "1");//0:专家指导,1:全程录像，operation为(0:开始录制)该参数有效
//        ApiService.handleGuide(new JSONObject(map))
//                .compose(RxSchedulersHelper.io_main())
//                .subscribe(new RxObserver<ResponseData>() {
//                    @Override
//                    public void _onSubscribe(Disposable d) {
//                        super._onSubscribe(d);
//                    }
//
//                    @Override
//                    public void _onNext(ResponseData responseData) {
//                    }
//
//                    @Override
//                    public void _onError(String errorMessage) {
//                    }
//                });
//
//    }
//
//
////    /*****声网监听******/
////    @Override
////    public void onFirstRemoteVideoDecoded(int uid, int width, int height, int elapsed) {
////
////    }
////
////    @Override
////    public void onUserJoined(int uid, int elapsed) {
////
////    }
////
////    @Override
////    public void onUserOffline(int uid, int reason) {
////
////    }
////
////    @Override
////    public void onUserMuteVideo(int uid, boolean muted) {
////
////    }
////
////    @Override
////    public void onJoinChannelSuccess(String channel, int uid, int elapsed) {
////
////        createGuide();
////
////    }
////
////
////    @Override
////    public void onLeaveChannel(IRtcEngineEventHandler.RtcStats stats) {
////        PrefUtils.setBoolean(this, PrefUtils.VIDEOTAPE, false);
////        Logger.i("打开相机1:"+request_code);
////        if (request_code == AppConstant.GET_SENSOR_DATA_CODE || request_code == AppConstant.COLOUR_DATA_CODE || request_code == AppConstant.ABNORMAL_CODE || request_code == AppConstant.ABNORMAL_GROUP_CODE || request_code == AppConstant.PROBLEM_FEEDBACK_CODE) {
////            Logger.i("推送:" + request_code);
////            if (request_code == AppConstant.ABNORMAL_CODE) {
////                if (flag) {
////                    flag = false;
////                    EventBus.getDefault().post(new VideoReleaseEvent(request_code));
////                }
////
////            } else {
////                EventBus.getDefault().post(new VideoReleaseEvent(request_code));
////            }
////
////        }
////
////    }
////
////    @Override
////    public void onError(int err) {
////
////    }
//
//    /*******end******/
//}
